package plg.grupo1.Lexico.StateMachineHierarchy;

import plg.grupo1.Lexico.TokenHierarchy.TokenError;
import plg.grupo1.Lexico.reserveWords.NoExisteSimbolo;
import plg.grupo1.Lexico.reserveWords.SimboloNoTieneValorNumerico;
import plg.grupo1.Lexico.reserveWords.ValorFueraRango;

public class State8 implements State{
	
    private static State8 INSTANCE = null;    
	private State8(){}
	
    private synchronized static void createInstance() {
        if (INSTANCE == null) INSTANCE = new State8();
    }
    
    public static State8 getInstance() {
        if (INSTANCE == null) createInstance();
        return INSTANCE;
    }
    
	public void goNext(){
		
		if(Contexto.isEof()) {
			try{
				Contexto.checkControlWord(Contexto.getLexema());
				Contexto.setCurrentState(State7.getInstance());
			}catch(NoExisteSimbolo e){
				Contexto.genErrorAndGoToInitialState("","Simbolo valido");
			}catch(SimboloNoTieneValorNumerico e){
				Contexto.genErrorAndGoToInitialState("","Simbolo sin valor numerico valido");
			}catch(ValorFueraRango e){
				Contexto.genErrorAndGoToInitialState("","Simbolo con valor numerico valido");
			}
		}
		else{
			
			char c = Contexto.getCharAndMoveToNext();//A
			
			if(c==' '){
				//TODO check control word
				Contexto.setCurrentState(State7.getInstance());
			}
			else if(Contexto.isEscape(c)){
				//TODO check control word
				Contexto.setCurrentState(State20.getInstance());
			}
			else if(c==';'){
				//Contexto.appendToLexema(c);//B
				Contexto.appendToControlWordDelim(c);//
				Contexto.setCurrentState(State10.getInstance());
			}
			else if(Character.isDigit(c)){
				//Contexto.appendToLexema(c);//B
				Contexto.appendToControlWordNum(c);//
				Contexto.setCurrentState(State12.getInstance());
			}
			else if(c=='-'){
				//Contexto.appendToLexema(c);//B
				Contexto.appendToControlWordNum(c);//
				Contexto.setCurrentState(State11.getInstance());
			}
			else if(Character.toLowerCase(c)>='a' && Character.toLowerCase(c)<='z'){
				Contexto.appendToLexema(c);//
			}
			else{
				//error
				Contexto.addError(new TokenError(
						"ERROR en linea "
							+ Contexto.getCurrentLineN()
						+ " expected ALL~[NUMEROS,' ',SIMBOLOS_NO_LETRA] but Found "
							+ Contexto.getLexema() + " + '" + c
							+ "'"));
				Contexto.clearLexema();
				Contexto.setCurrentState(State0.getInstance());
			}
		}
	}
}